import { ref } from 'vue'
import { adapter, request } from '@/request'
import { activityUpdateByPost } from '@/core/api/activity'
import type { FreeWordsRules } from '@/services/activity/getActivity'
import type { Activity } from '@/core/interfaces/activity'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Mode } from '@/core/interfaces/common'
import type { ComboPriceNameEnum } from '@/core/enums/name'

export interface ActivityFields {
  id?: number
  startTime: Activity['startTime']
  endTime: Activity['endTime']
  type: Activity['type']
  status: Activity['status']
  highestHeat?: number
  // productType?: string
  freeWordRules: Partial<
    Pick<
      FreeWordsRules,
      'key' | 'highPrice' | 'lowPrice' | 'combinedWordNum' | 'wordNum' | 'activityId' | 'pcImgNum' | 'ydImgNum'
    >
  >[]
  daysType?: ComboPriceNameEnum
  daysbanben?: ComboPriceNameEnum
}

export default function updateActivity() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)

  const fetch = (data: ActivityFields, mode: Mode) => {
    const { url, method } = activityUpdateByPost(mode)
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [null, fetch, loading] as const
}
